<?php

namespace App\Http\Controllers\Distribution;

use Illuminate\Http\Request;
use App\Models\Video;
use App\Models\DisCourse;
use App\Models\DisCourseClass;
use App\Models\DisForm;
use App\Models\UserGrowing;
use App\Models\UserAttribute;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;
use App\User;
use App\Utils\WxMessagePush;
use App\Contant\WxMessageType;
use App\Events\WxCustomerMessagePush;

class DisController extends Controller
{
	protected $ret;

    public function __construct()
    {
        $this->ret = [];
    }

    //20190403 分销打卡视频列表
    public function video(Request $request)
    {	
		$DisCourse = new DisCourse();
		$arr = $request->input("title");
		//dd($arr);
		if(Input::has('title')){
			$data = DB::table("dis_course")->where("title","like","%$arr%")->paginate(10);
		}else{
			$data = $DisCourse->getList();
		}
        
        //dd($data);
        return view("dis.index",['list'=>$data,'title'=>$arr]);
    }

    //20190403 分销打卡添加视频页面
    public function videoAdd(Request $request)
    {	
		$this->ret['about'] = false;
        return view("dis.videoadd");
    }

    //20190403 分销打卡添加视频操作
    public function videoInsert(Request $request){
        $data = $request->input();
        $id   = $request->input("id");
        if($id){
            $course = DisCourse::where("id", $data['id'])->first();
            $course->opertion_user_id = $request->user()->id;
        }else{
            $course = new DisCourse();
            $course->opertion_user_id = $request->user()->id;
        }
        
        $course->title   = $data['title'];
        $course->orderby = $data['orderby'];
        $course->delay   = $data['delay'];
        $course->long_time   = $data['long_time'];
        $course->video_url   = $data['video_url'];
        $course->cover_url   = $data['cover_url'];
        $course->hotwords    = $data['hotwords'];
        $course->description = $data['description'];
            
        $result = $course->save();
        if($result){
            return redirect('/dis/video');
        }
    }

    //20190403 分销打卡视频编辑页面
    public function videoUpdate(Request $request, $id){
        
        if($id){
            $course = DisCourse::where("id", $id)->first();
        }
        
        return view("dis.videoupdate",['item'=>$course]);
    }

    //20190403 分销打卡视频删除操作
    public function videoDelete(Request $request){
    	$id = $request->input("id");
        $result = DisCourse::where('id',$id)->delete();

        if($result){
            return $this->getMessage(0,'删除成功');
        }else{
            return $this->getMessage(1,'删除失败');
        }
    }

    //20190403 打卡课程列表
    public function course(Request $request){
    	$DisCourseClass = new DisCourseClass();
		$arr = $request->input("title");
		//dd($arr);
		if(Input::has('title')){
			$data = DB::table("dis_course_class")->where("title","like","%$arr%")->paginate(10);
		}else{
			$data = $DisCourseClass->getList();
		}
        
        //dd($data);
        return view("dis.course",['list'=>$data,'title'=>$arr]);
    }


    //20190403 课程顾问管理
    public function distributor(Request $request){
    	
        $DisForm = new DisForm();
        $arr = $request->input("title");
        $code= $request->input("code", 0);
        //dd($arr);
        if(Input::has('title')){
            $data = DB::table("dis_form")->where("name","like","%$arr%")->where("code", $code)->orderBy("updated_at", "desc")->paginate(10);
        }else{
            $data = $DisForm->getList($code);
        }
        
        //dd($data);
        return view("dis.form.formlist",['list'=>$data,'title'=>$arr, "code"=>$code]);
    }

    //20190403 分销打卡添加课程页面
    public function courseAdd(Request $request)
    {	
		
        return view("dis.course.courseadd");
    }

    //20190404 分销打卡添加视频操作
    public function courseInsert(Request $request){
        $data = $request->input();
        $id   = $request->input("id");
       
        if($id){
            $courseClass = DisCourseClass::where("id", $data['id'])->first();
            $courseClass->opertion_user_id = $request->user()->id;
        }else{
            $courseClass = new DisCourseClass();
            $courseClass->opertion_user_id = $request->user()->id;
        }
        
        $courseClass->title   = $data['title'];
        $courseClass->orderby = $data['orderby'];
        $courseClass->price   = $data['price'];
        $courseClass->o_price = $data['o_price'];
        $courseClass->state   = $data['state'];
        $courseClass->course_num  = $data['course_num'];
        $courseClass->course_ids  = $data['course_ids'];
        $courseClass->cover_url   = $data['cover_url'];
        $courseClass->poster_url  = $data['poster_url'];
        $courseClass->description = $data['description'];
            
        $result = $courseClass->save();
        if($result){
            return redirect('/dis/course');
        }
    }

    //20190403 分销打卡视频删除操作
    public function courseDelete(Request $request){
        $id = $request->input("id");
        $result = DisCourseClass::where('id',$id)->delete();

        if($result){
            return $this->getMessage(0,'删除成功');
        }else{
            return $this->getMessage(1,'删除失败');
        }
    }

    //20190408 分销打卡课程编辑页面
    public function courseUpdate(Request $request, $id){
        
        if($id){
            $courseClass = DisCourseClass::where("id", $id)->first();
        }
        
        return view("dis.course.courseupdate",['item'=>$courseClass]);
    }

    /*
     * 关联视频
     */
    public function selectDisCourse(Request $request){
        $DisCourse = new DisCourse();
        $title = $request->input('title','');
        if($title){
            $DisCourse  = $DisCourse->where('title','like',"%$title%");
        }
        $DisCourse = $DisCourse->paginate(5);

        return json_encode(['code'=>0,'body'=>view('dis.course.courselist',['course'=>$DisCourse,'title'=>$title])->render()]);
    }

    //20190403 课程顾问审核通过/拒绝通过
    public function formCode(Request $request){
        $id   = $request->input("id");
        $code = $request->input("code");
        $remark = $request->input("remark");
        $DisForm = new DisForm();

         //开启事务
        DB::beginTransaction();
        try{
            if($id){
                $item = $DisForm->where("id", $id)->first();
                $item->code = $code;
                $item->remark = $remark;
                $result = $item->save();
            }
            if($result){
                if($code==1){
                    //和老师绑定关系
                    $teacher_user_growing = UserGrowing::where("mobile", $item->teacher_mobile)->first();
                    if(!$teacher_user_growing){
                        return json_encode(["code"=>0, "msg"=>"关联导师手机号不存在"]);
                    }
                    $cur_user_growing = UserGrowing::where("user_id", $item->user_id)->first();
                    $cur_user_growing->fission_id= $teacher_user_growing->user_id;
                    $cur_user_growing->save();

                    //表示通过审核，修改他的身份
                    $attribute = UserAttribute::where("user_id", $item->user_id)->first();
                    $attribute->is_fission = 2;
                    $attribute->save();
                }
                //审核结果通知
                $user = DB::table("users")->where("id", $item->user_id)->select("id", "openid", "nickname")->first();
                if($user && $user->openid){
                    $data['type'] = WxMessageType::RESULT;
                    $data['url'] = env('PT_WEB_URL')."/distribution/apply.html";
                    $data['notice'] = '您申请成为课程顾问结果通知';
                    $data['message']['result']  = $code==1 ? "申请通过" : "申请失败";
                    $data['message']['nickname']= $user->nickname;
                    $data['message']["remark"]  = $remark;
                   
                    $data['openid'] = $user->openid;
                    $wxpush = new WxMessagePush();
                    $wxpush->sendMessage($data); 
                    
                }
                DB::commit();
                return json_encode(["code"=>1, "msg"=>"审核通过"]);
            }else{
                DB::commit();
                return json_encode(["code"=>0, "msg"=>"操作失败请稍后重试"]);
            }
        }catch(\Exception $e){
            logger()->info($e->getMessage());
            DB::rollback();
        }
    }

    //20190403 课程顾问关联老师手机号
    public function formMobile(Request $request){
        $id   = $request->input("id");
        $mobile = $request->input("mobile");
        $DisForm = new DisForm();
        if($id){
            $item = $DisForm->where("id", $id)->first();
            $item->teacher_mobile = $mobile;
            $result = $item->save();
        }
        if($result){
            return json_encode(["code"=>1, "msg"=>"修改成功"]);
        }else{
            return json_encode(["code"=>0, "msg"=>"操作失败请稍后重试"]);
        }
    }

}
